﻿@model BusinessManagementStudio.ViewModel.CompanyRealOrder

@{
    ViewBag.Title = "Quotation And Invoice Enquiry";
    TempData.Keep();
}

<title>Quotation And Invoice Enquiry</title>

<h2 align="center">Quotation And Invoice Enquiry</h2>

@using (Html.BeginForm("Switch", "QuotationAndInvoice"))
{
    <table align="center">
        <tr></tr>
        <tr>
            <td width="120">Select A Company</td>
            <td>@Html.DropDownList("Company", Model.company.CompanyNameList, null, new
                                    {
                                        title = "Pick Batch",
                                        onchange = "submit();"
                                    })</td>
        </tr>
        <tr></tr>
    </table>
}

@using (Html.BeginForm("MultipleCommand", "Order", FormMethod.Post))
{    
    <div align="center"><button type="submit" name="Command" value="BackToMainMenu">Back To Main Menu</button></div>
}

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <table align="center">
        <tr>
            <td width="150">FM :</td>
            <td>@Html.DropDownList("FMCode", Model.fm.FMNameList)</td>
        </tr>
        <tr>
            <td width="120">Quotation No. :</td>
            <td>@Html.TextBox("QuotationNo")</td>
            <td><button type="submit" name="Search" value="Search">Search</button></td>
        </tr>
    </table>
    
    <br align="center"/>
    if (Model.orderList != null)
    {
        <table align="center" border="1" id="tblJLLPO">
            <tr class="tableHeader">
                <th width="100">FM Code</th>
                <th width="150">Date</th>
                <th width="120">Quotation No.</th>
                <th width="250">Location</th>
                <th width="10">Valid</th>
                <th width="100">Quotation</th>
                <th width="100">Invoice</th>
            </tr>
            @foreach (var item in Model.orderList)
            {
                <tr >
                    <td align="center">@item.FMCode</td>
                    <td align="center">@item.CreationDate.ToString("yyyy-MM-dd")</td>
                    <td align="center">@item.QuotationNo</td>
                    <td align="center">@item.Location</td>
                    @*<td align="center">@Html.CheckBox("Valid", true, new { id = "CBox" })</td>*@
                    @if(item.IsValid == true)
                    {
                        <td align="center"><input type="checkbox" name="ckCheck" id='@("ckCheck_" + @item.QuotationNo)' checked="checked"/></td>
                    }
                    else
                    {
                        <td align="center"></td>
                    }
                    @if (item.HasQuotation == true && item.IsValid == true)
                    {
                        <td align="center">@Html.ActionLink(@item.QuotationNo, "JLLPOQuotation_View", "QuotationAndInvoice", new { QuotationNo = @item.QuotationNo }, new { @class = "openDialog2", data_dialog_id = "aboutlDialog2", data_dialog_title = "JLL-(PO) Quotation" })</td>
                    }
                    else if (item.HasQuotation == false && item.IsValid == true)
                    {
                        <td align="center">@Html.ActionLink("Add", "JLLPOQuotation", "QuotationAndInvoice", new { QuotationNo = @item.QuotationNo }, new { @class = "openDialog2", data_dialog_id = "aboutlDialog2", data_dialog_title = "JLL-(PO) Quotation" })</td>
                    }
                    else
                    {
                        <td align="center"></td>
                    }
                    @if (item.HasInvoice == true && item.IsValid == true)
                    {
                        <td align="center">@Html.ActionLink(@item.InvoiceNo, "JLLPOInvoice_View", "QuotationAndInvoice", new { QuotationNo = @item.QuotationNo }, new { @class = "openDialog2", data_dialog_id = "aboutlDialog2", data_dialog_title = "JLL-(PO) Invoice" })</td>
                    }
                    else if (item.HasInvoice == false && item.IsValid == true)
                    {
                        <td align="center">@Html.ActionLink("Add", "JLLPOInvoice", "QuotationAndInvoice", new { QuotationNo = @item.QuotationNo }, new { @class = "openDialog2", data_dialog_id = "aboutlDialog2", data_dialog_title = "JLL-(PO) Invoice" })</td>
                    }
                    else
                    {
                        <td align="center"></td>
                    }
                </tr>
            }
        </table>
    }
    
    @*<div id="someDivId">This is some div</div>*@
}

<link href='~/Scripts/jquery-ui.css' rel='stylesheet'>
<script type="text/javascript" src="~/Scripts/jquery-2.1.0.js"></script>
<script type="text/javascript" src="~/Scripts/jquery-ui.js"></script>
<script type="text/javascript">
    $.ajaxSetup({ cache: false });

    $(document).ready(function () {
        $(".openDialog2").click(function (e) {
            e.preventDefault();

            $("<div></div>")
                    .addClass("dialog")
                    .attr("id", $(this)
                    .attr("data-dialog-id"))
                    .appendTo("body")
                    .dialog({
                        title: $(this).attr("data-dialog-title"),
                        close: function () { $(this).remove(); },
                        modal: true,
                        height: 800,
                        width: 900,
                        left: 0

                    })
                    .load(this.href);
        });

        $(".close").click(function (e) {
            e.preventDefault();
            $(this).closest(".dialog").dialog("close");
        });

        $(":checkbox").change(function (e) {
            var parts = e.target.id.split('_');
            var lastChar = e.target.id.substr(e.target.id.length - 1);
            var quoteNo = parts[1];
            var isChecked = ($(e).closest('td').find($('#ckCheck_' + quoteNo))).is(':checked');
            if (isChecked == false) {
                var fmCode = $(this).closest('td').prev('td').prev('td').prev('td').prev('td').text();
                var nQuoteNo = $(this).closest('td').prev('td').prev('td').text();

                $.ajax({
                    url: '@Url.Action("JLLPOQuotationNoModification", "QuotationAndInvoice")',
                    type: "POST",
                    dataType: "text",
                    data: ({ FMCode: fmCode, QuotationNo: quoteNo }),
                    success: function (result) {
                        alert(result);
                        window.location = '@Url.Action("Index", "QuotationAndInvoice")';
                    }
                });
            }
        });
    });
</script>